﻿using Microsoft.AspNetCore.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace IceDog.WebApi.AnythingApi.Mapping
{
    public static class ContentTypeHelper
    {
        /// <summary>
        /// 
        /// </summary>
        public static string DefaultMineType { get; } = "application/octet-stream";

        /// <summary>
        /// 映射集合
        /// </summary>
        public static IDictionary<string, string> Mappings { get; private set; }
        static ContentTypeHelper()
        {
            var filePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "mine-types.json");
            var jsonText = File.ReadAllText(filePath);
            Mappings = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonText);
        }

        /// <summary>
        /// 获取扩展名
        /// </summary>
        /// <param name="extension">e.g. .html  .js</param>
        /// <returns></returns>
        public static string GetMimeType(string extension)
        {
            extension = extension.ToLower();
            if (string.IsNullOrWhiteSpace(extension))
            {
                return DefaultMineType;
            }
            if (!Mappings.ContainsKey(extension))
            {
                return DefaultMineType;
            }
            return Mappings[extension];
        }
    }
}